package org.huntaway.lucene.document.impl;

import java.io.File;
import java.io.StringReader;

import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.huntaway.lucene.document.BaseDocument;
import org.huntaway.lucene.po.FileBean;

public class FileDocument<T extends FileBean> extends BaseDocument<FileBean> {
	public void setBackField(File fileName) {
		doc.add(new Field(T.REF_NAME, fileName.getName(), Field.Store.YES,
				Field.Index.NOT_ANALYZED));
		doc.add(new Field(T.REF_PATH, fileName.getPath(), Field.Store.YES,
				Field.Index.NOT_ANALYZED));
		doc.add(new Field(T.REF_MODIFIED, DateTools.timeToString(
				fileName.lastModified(), DateTools.Resolution.MINUTE),
				Field.Store.YES, Field.Index.NOT_ANALYZED));
	}

	protected void setAnalyzerField(File fileName) {
		doc.add(new Field(T.REF_NAME, new StringReader(fileName
				.getName())));
	}

	public FileBean getBackField(Document doc) {
		FileBean filePo = new FileBean();
		filePo.setName(doc.get(FileBean.REF_NAME));
		filePo.setPath(doc.get(FileBean.REF_PATH));
		filePo.setModified(doc.get(FileBean.REF_MODIFIED));
		return filePo;
	}
}
